package cn.changeforyou.utils.convert.twoDimensionalTable;

import cn.hutool.core.util.BooleanUtil;

import java.io.UnsupportedEncodingException;

/**
 * 二维表工具
 *
 * @version 1.0
 * @description
 * @auther zhyu
 * 2022-9-23 10:18
 */
public class TwoDimensionalTableUtil {

    static String systemDefaultCharset = System.getProperty("file.encoding");


    public static String defaultValue(Object obj) {
        return defaultValue(obj, systemDefaultCharset);
    }

    public static String defaultValue2String(String value) {
        if ("-".equals(value)) {
            return null;
        }
        return value;
    }

    public static Boolean defaultValue2Boolean(String value) {
        if ("-".equals(value)) {
            return null;
        }
        return cn.hutool.core.util.BooleanUtil.toBoolean(value);
    }

    public static String defaultValue(Object obj, String charset) {
        if (null == obj) {
            return "-";
        }
        if (BooleanUtil.isBoolean(obj.getClass())) {
            return convertCharset(cn.hutool.core.util.BooleanUtil.toStringYesNo((Boolean) obj), charset);
        } else {
            return convertCharset(obj.toString(), charset);
        }
    }

    private static String convertCharset(String value, String charset) {
        try {
            return new String(value.getBytes(), systemDefaultCharset);
        } catch (UnsupportedEncodingException e) {
            throw new RuntimeException(e);
        }
    }
}
